package com.xbfinal.reggie.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbfinal.reggie.common.R;
import com.xbfinal.reggie.dto.SetmealDto;
import com.xbfinal.reggie.pojo.Category;
import com.xbfinal.reggie.pojo.Setmeal;
import com.xbfinal.reggie.service.CategoryService;
import com.xbfinal.reggie.service.SetmealService;
import com.xbfinal.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 *
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @CacheEvict(value = "setmealCache",key = "#setmealDto.categoryId")
    public R<String> save(@RequestBody SetmealDto setmealDto){

        //套餐表插入数据  套餐关系表也要插入数据
        setmealService.saveWitDish(setmealDto);
        return R.success("添加成功");
    }

    /**
     * 更新套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    @CacheEvict(value = "setmealCache",key = "#setmealDto.categoryId")
    public R<String> saveUpdate(@RequestBody SetmealDto setmealDto){
        //套餐表插入数据  套餐关系表也要插入数据
        setmealService.updateWithDish(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Long page,Long pageSize,String name){

        //分页构造器
        final Page<Setmeal> objectPage = new Page<>(page,pageSize);
        final Page<SetmealDto> DtoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper
                =new LambdaQueryWrapper<>();
        //添加查询条件
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(objectPage,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(objectPage,DtoPage,"records");
        final List<Setmeal> records = objectPage.getRecords();
        List<SetmealDto> list=records.stream().map(item->{
            SetmealDto setmealDto=new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            final Long categoryId = item.getCategoryId();
            //根据分类的id查询
            final Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                setmealDto.setCategoryName(byId.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        DtoPage.setRecords(list);
        return R.success(DtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache" ,allEntries = true)//allEntries = true清理setmealCache下所有的缓存
    public R<String> delete(@RequestParam List<Long> ids){
        //删除套餐还应该删除对应的关联关系
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 修改套餐
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @CacheEvict(value = "setmealCache" ,allEntries = true)
    public R<SetmealDto> update(@PathVariable Long id){
        //log.info("{}",id);
        final SetmealDto setmealDto = setmealService.updateWithDish(id);
        return R.success(setmealDto);
    }


    /**
     *停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> stop(@RequestParam List<Long> ids){
        //这里我们使用@RequestParam参数
         Setmeal setmeal = new Setmeal();
        for (long id: ids ) {
            setmeal.setStatus(0);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }

        return R.success("修改成功");
    }

    /**
     *起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> starting(@RequestParam List<Long> ids){
        //这里我们使用@RequestParam参数
        Setmeal setmeal = new Setmeal();
        for (long id: ids ) {
            setmeal.setStatus(1);
            setmeal.setId(id);
            setmealService.updateById(setmeal);
        }

        return R.success("修改成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId")
    public R<List<Setmeal>> listR(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper
                =new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        final List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }
}
